package com.briup.MR.Sort.SecondSort;


import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class YearTemp implements WritableComparable<YearTemp> {
    private String year;
    private double temp;

    @Override
    public String toString() {
        return "YearTemp{" +
                "year='" + year + '\'' +
                ", temp=" + temp +
                '}';
    }

    public YearTemp(String year, double temp) {
        this.year = year;
        this.temp = temp;
    }

    public YearTemp() {
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public double getTemp() {
        return temp;
    }

    public void setTemp(double temp) {
        this.temp = temp;
    }

    public int compareTo(YearTemp o) {
        //this 1991 o->1992
        //this新加入的元素，o是排好顺序的
        //结果得到负值，this插入到o元素的前面，升序
        int n=this.getYear().compareTo(o.year);
        if(n!=0){
            return -n;
        }
        return (int) (this.temp-o.getTemp());
    }

    public void write(DataOutput out) throws IOException {
        out.writeUTF(year);
        out.writeDouble(temp);
    }

    public void readFields(DataInput in) throws IOException {
        this.year=in.readUTF();
        this.temp=in.readDouble();
    }
}
